package org.yunmai.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.yunmai.entity.SysUser;
import org.yunmai.vo.SysUserVO;

import java.util.List;

/**
* @author Administrator
* @description 针对表【sys_user】的数据库操作Mapper
* @createDate 2025-06-03 19:48:01
* @Entity org.yunmai.entity.SysUser
*/
@Mapper
public interface SysUserMapper extends BaseMapper<SysUser> {



    @Select({
            "<script>",
            "SELECT u.id, u.username, u.realname, d.name AS deptName, GROUP_CONCAT(r.role_name) AS roleName",
            "FROM sys_user u",
            "LEFT JOIN sys_dept d ON u.dept_id = d.id",
            "LEFT JOIN sys_user_role ur ON u.id = ur.user_id",
            "LEFT JOIN sys_role r ON ur.role_id = r.id",
            "WHERE 1=1",
            "<if test='keyword != null and keyword != \"\"'>",
            "   AND (u.username LIKE CONCAT('%', #{keyword}, '%') ",
            "   OR u.realname LIKE CONCAT('%', #{keyword}, '%') ",
            "   OR r.role_name LIKE CONCAT('%', #{keyword}, '%')",
            "   OR d.name LIKE CONCAT('%', #{keyword}, '%'))",
            "</if>",
            "GROUP BY u.id, u.username, u.realname, d.name",
            "LIMIT #{offset}, #{pageSize}",
            "</script>"
    })
    List<SysUserVO> selectUserPage(@Param("offset") int offset, @Param("pageSize") int pageSize, @Param("keyword") String keyword);

    @Select({
            "<script>",
            "SELECT COUNT(*) FROM sys_user u",
            "LEFT JOIN sys_dept d ON u.dept_id = d.id",
            "WHERE 1=1",
            "<if test='keyword != null and keyword != \"\"'>",
            "AND (u.username LIKE CONCAT('%', #{keyword}, '%') OR d.name LIKE CONCAT('%', #{keyword}, '%'))",
            "</if>",
            "</script>"
    })
    int countUserByKeyword(String keyword);
}




